import fg from 'fast-glob';
import fs from 'fs';


const exec = async () => {
    const entries = await fg(['src/*.js']);

    // 如果入口文件已经存在，则删除它，
    const arr = await fg(['./index.js']);
    if (arr.length) {
        fs.unlinkSync('./index.js');
    }

    // 入口文件里的代码用文件追加方式一句句生成
    const keyArr = []
    for (let key in entries) {
      console.log('key', entries[key]);
        const a = entries[key].replace('.js', '')
        const keyword = a.replace('src/', '')
        keyArr.push(keyword)
        fs.writeFile('./index.js', `import ${keyword} from './${entries[key]}';\n`, {'flag':'a'}, function (err) {
            if (err) throw err;
        });
    }
    const keys = keyArr.join(',')
    console.log('export');
    fs.writeFile('./index.js', `export default { ${keys} }`, {'flag':'a'}, function (err) {
        if (err) throw err;
    });

};

exec();

